package br.com.novavendas.dao;

import java.util.List;

import javassist.bytecode.Opcode;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;


import br.com.novavendas.model.Cliente;

public class DaoCliente extends Dao<Cliente> implements IDao<Cliente>{

	public DaoCliente() {
		super(Cliente.class);
	}

	@Override
	public void salva(Cliente objeto) throws Exception {
		Session session = super.getSession();
		Transaction tx = session.beginTransaction();
		try{	
			if (objeto.getId() == null)
				session.save(objeto);
			else
				session.update(objeto);
			tx.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}finally{
			session.close();
		}	
	}

	@Override
	public void deleta(Cliente objeto) throws Exception {
		Session session = super.getSession();
		Transaction tx = session.beginTransaction();
		try{
			session.delete(objeto); 
			tx.commit();
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}finally{
			session.close();
		}
		
	}

	@Override
	public Cliente getById(Cliente objeto) throws Exception {
		Session session = super.getSession();
		try{
			Query query=session.createQuery("select * from loja.clientes where id=?");
			return (Cliente) query.setLong(1, objeto.getId()).uniqueResult();
 
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}finally{
			session.close();
		}
	}

	@Override
	public List<Cliente> listagem() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Cliente> listagemNome(String nome) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
//	public List<Pedido> listagemByFabricante(Fabricante fabricante) throws Exception{
//		Session session = getSession();
//		try{
//			Query query = session.createQuery("select p From Pedido p, ItemPedido i where i.pedido.id = p.id and i.produto.fabricante.id = " + fabricante.getId());
//			return query.list();
//		}catch(Exception ex){
//			throw ex;			
//		}finally{
//			session.close();
//		}		
//	}

}
